﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Model;
using System.ComponentModel;
using YiYiTong.Rocket.Model.Manager;
using YiYiTong.Rocket.Business;

namespace YiYiTong.Rocket.ManageWeb.Controllers
{
    public class BrandController : BaseController
    {
        [Description("品牌列表")]
        [ViewPage]
        public ActionResult BrandList(BrandListQueryModel queryModel)
        {
            queryModel.Trim();
            ViewBag.paramModel = queryModel;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //条件筛选
            var queryable = dbcontext.ADZ_BrandMaster.Where(t => t.is_del == 0);
            if (!string.IsNullOrWhiteSpace(queryModel.brand_name))
                queryable = queryable.Where(t => t.brand_name.Contains(queryModel.brand_name));
            if (!string.IsNullOrWhiteSpace(queryModel.brand_name_en))
                queryable = queryable.Where(t => t.brand_name_en.Contains(queryModel.brand_name_en));
            if (queryModel.is_show != -10)
                queryable = queryable.Where(t => t.is_show == queryModel.is_show);

            //分页
            int rowcounts = queryable.Count();
            ViewData.Add("AllCount", rowcounts);
            var querylist = queryable.OrderByDescending(t => t.sort_NO).ThenByDescending(t => t.brand_id)
                .Skip(queryModel.pageSize * (queryModel.pageIndex - 1))
                .Take(queryModel.pageSize)
                .ToList();

            PagedList<ADZ_BrandMaster> pagedlist = null;
            if (querylist != null && querylist.Count != 0)
                pagedlist = new PagedList<ADZ_BrandMaster>(querylist, queryModel.pageIndex, queryModel.pageSize, rowcounts);
            return View(pagedlist);
        }

        [Description("新增/编辑品牌")]
        [ViewPage]
        public ActionResult Add(int id = 0)
        {
            ViewBag.Title = "新增品牌";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            ADZ_BrandMaster supplier = new ADZ_BrandMaster();
            if (id > 0)
            {
                ViewBag.Title = "编辑品牌";
                supplier = dbcontext.ADZ_BrandMaster.FirstOrDefault(t => t.brand_id == id && t.is_del == 0);
            }
            return View(supplier);
        }

        [Description("新增/编辑品牌")]
        [HttpPost]
        public ActionResult Add(ADZ_BrandMaster brand)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            //参数验证
            if (string.IsNullOrWhiteSpace(brand.brand_name))
            {
                resMsg.Message = "品牌名称不能为空";
                return Json(resMsg, JsonRequestBehavior.AllowGet);
            }

            //执行新增or编辑
            Tuple<bool, string> result = null;
            if (brand.brand_id > 0)
                result = GoodsBiz.Instance.EditBrand(brand);
            else
                result = GoodsBiz.Instance.AddBrand(brand);

            resMsg.IsSuccess = result.Item1;
            resMsg.Message = result.Item2;
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("删除品牌")]
        [HttpPost]
        public ActionResult ToDel(int id = 0)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var entity = dbcontext.ADZ_BrandMaster.FirstOrDefault(t => t.brand_id == id);
            if (entity != null)
            {
                entity.is_del = 1;
                dbcontext.SaveChanges();
                resMsg.IsSuccess = true;
                resMsg.Message = "操作成功";
            }
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }

        [Description("批量检查品牌")]
        [ViewPage]
        public ActionResult BatchCheck()
        {
            ViewBag.Title = "批量检查品牌是否存在";
            return View();
        }

        [Description("批量检查品牌")]
        [HttpPost]
        public ActionResult BatchCheck(string brand_names)
        {
            ResponseMsg resMsg = new ResponseMsg();
            resMsg.IsSuccess = false;
            resMsg.Message = "操作失败";

            string[] brandNameList = StringHelper.Split(brand_names, '\n');
            string result = "";
            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            foreach (var item in brandNameList)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    var flag = dbcontext.ADZ_BrandMaster.Where(t => t.brand_name == item).ToList();
                    if (flag == null || flag.Count == 0)
                        result += item + "：不存在\n";
                    else
                        result += item + "：存在\n";
                }
            }
            resMsg.IsSuccess = true;
            resMsg.Body = result;
            resMsg.Message = "操作成功";
            return Json(resMsg, JsonRequestBehavior.AllowGet);
        }
    }
}